package com.panfeng.xcloud.boss.provider.assets.service.exchangeTest.impl;

import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.boss.provider.assets.service.exchangeTest.IExchangeMethodServiceTest;
import com.panfeng.xcloud.boss.provider.dto.request.OperateBotReqDTO;
import com.panfeng.xcloud.common.core.enums.ExchangeTypeEnum;
import com.panfeng.xcloud.common.core.utils.KeyContactUtil;
import com.panfeng.xcloud.common.core.web.vo.InnerTransferVO;
import com.panfeng.xcloud.common.core.web.vo.OrderDetailResponseVO;
import com.panfeng.xcloud.common.core.web.vo.TradeResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class HuobiMethodServiceTest implements IExchangeMethodServiceTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询账户总可用余额
     *
     * @param userId
     * @param valuationAsset
     * @return
     */
    @Override
    public BigDecimal getTotalAvaliableMoney(String userId, String valuationAsset) {
        BigDecimal getTotalAvaliableMoney = new BigDecimal(String.valueOf(redisTemplate.opsForValue().get("test:getTotalAvaliableMoney")));
        return getTotalAvaliableMoney;
    }

    /**
     * 查询某交易对保证金率
     *
     * @param userId
     * @param contractCode
     * @return
     */
    @Override
    public BigDecimal getRiskRate(String userId, String contractCode) {
        BigDecimal getRiskRate = new BigDecimal(String.valueOf(redisTemplate.opsForValue().get("test:getRiskRate")));
        return getRiskRate;
    }

    /**
     * 查询某交易对的可用仓位
     *
     * @param userId
     * @param contractCode
     * @param direction
     * @param leverRate
     * @return
     */
    @Override
    public BigDecimal getAvaliablePosition(String userId, String contractCode, String direction, Integer leverRate) {
        BigDecimal getAvaliablePosition = new BigDecimal(String.valueOf(redisTemplate.opsForValue().get("test:getAvaliablePosition")));
        return getAvaliablePosition;
    }

    /**
     * 查询某交易对的可用余额
     *
     * @param userId
     * @param contractCode
     * @return
     */
    @Override
    public BigDecimal getAvaliableMoney(String userId, String contractCode) {
        BigDecimal getAvaliableMoney = new BigDecimal(String.valueOf(redisTemplate.opsForValue().get("test:getAvaliableMoney")));
        return getAvaliableMoney;
    }

    /**
     * 查询某交易对的可开张数
     *
     * @param userId
     * @param destinationCoin
     * @param sourceCoin
     * @param direction
     * @param leverRate
     * @return
     */
    @Override
    public BigDecimal getAvaliablePieces(String userId, String destinationCoin, String sourceCoin, String direction, Integer leverRate) {
        BigDecimal getAvaliablePieces = new BigDecimal(String.valueOf(redisTemplate.opsForValue().get("test:getAvaliablePieces")));
        return getAvaliablePieces;
    }

    /**
     * 交易所下市价单
     *
     * @param operateBotReqDTO
     * @return
     */
    @Override
    public TradeResponseVO trade(OperateBotReqDTO operateBotReqDTO) {
        TradeResponseVO tradeResponseVO = JSONObject.parseObject(stringRedisTemplate.opsForValue().get("test:trade"), TradeResponseVO.class);
        return tradeResponseVO;
    }

    /**
     * 获取订单详情
     *
     * @param operateBotReqDTO
     * @param tradeResponseVO
     * @return
     */
    @Override
    public OrderDetailResponseVO getOrderDetail(OperateBotReqDTO operateBotReqDTO, TradeResponseVO tradeResponseVO) {
        OrderDetailResponseVO orderDetailResponseVO = JSONObject.parseObject(stringRedisTemplate.opsForValue().get("test:getOrderDetail"), OrderDetailResponseVO.class);
        return orderDetailResponseVO;
    }

    /**
     * 同账户内资金划转
     *
     * @param innerTransferVO
     * @return
     */
    @Override
    public String transferInner(InnerTransferVO innerTransferVO) {
        String transferInner = String.valueOf(redisTemplate.opsForValue().get("test:transferInner"));
        return transferInner;
    }

    /**
     * 获取展示市价
     *
     * @param userId
     * @param destinationCoin
     * @param sourceCoin
     * @return
     */
    @Override
    public BigDecimal getAppearMarketPrice(String userId, String destinationCoin, String sourceCoin) {
        String closePrice = String.valueOf(redisTemplate.opsForValue().get("test:getAppearMarketPrice"));
        redisTemplate.opsForValue().set(KeyContactUtil.getAppearMarketKey(ExchangeTypeEnum.HUOBI.getCode(), destinationCoin, sourceCoin), closePrice);
        return new BigDecimal(closePrice);
    }

    /**
     * 获取行情深度,供计算使用
     *
     * @param userId
     * @param destinationCoin
     * @param sourceCoin
     * @return
     */
    @Override
    public JSONObject getMarketPriceDepth(String userId, String destinationCoin, String sourceCoin) {
        JSONObject getMarketPriceDepth = JSONObject.parseObject(stringRedisTemplate.opsForValue().get("test:getMarketPriceDepth"), JSONObject.class);
        return getMarketPriceDepth;
    }

}
